from apps.agregator import api_agregator
from xml.dom import minidom
from xml.dom.minidom import Document
from django.http import HttpResponse

from format.utils import generateResponse

from photoLoader import *
from videoLoader import *
from placeLoader import *
from geoNodeGenFactory import *

import copy

#def error (message):
#	doc = Document()
#	hijo1 = doc.createElement("error")
#	doc.appendChild(hijo1)
#	desc = doc.createElement("description")
#	hijo1.appendChild(desc)
#	texto = doc.createTextNode(message)
#	desc.appendChild(texto)
#	mime_type = "text/xml"
#	return HttpResponse(doc.toxml(), mime_type)

#MANDATORY_FIELDS = {}
#MANDATORY_FIELDS['panoramio'] = ["latitude", "longitude", "radius"]
#MANDATORY_FIELDS['geonames'] = ["latitude", "longitude", "radius"]
#MANDATORY_FIELDS['11870'] = ["latitude", "longitude", "radius",'text']
#MANDATORY_FIELDS['youtube'] = ["latitude", "longitude", "radius",'text']


#this function is for testing the agregator
def generate_XML_response (request):
	response = None	
	params = {}
	xmlPath = None
	channel = None
	category = None
	format = getResponseFormat (request) 
	#we get from the petition the parameters
	if request.method == "POST":
		if "fromx" in request.POST:
			params['fromx'] = request.POST['fromx']
	if request.method == "POST":		
		if "toy" in request.POST:
			params['toy'] = request.POST['toy']
	if request.method == "POST":
		if "channel" in request.POST:
			channel = request.POST['channel']		
			if "category" in request.POST:
				category = request.POST['category']
				channel = channel.lower()
				for field2 in request.POST:
						params[field2] = request.POST[field2]
				#after getting the parameters we generate the xml file from the external resource		
				xmlPath ,response, params2 = api_agregator.generate_XML(channel, category, params)
				#we manage the errors that we get
				if (xmlPath == None):
					list = response.split(" ")
					response =  error(format,"Wrong Post Parameters, " + list[1] + " missing")
			else:
				response =  error (format,"Wrong Post Parameters, category missing")
		else:
			response =  error (format,"Wrong Post Parameters, channel missing")														
	return response

#############################################################################################################################################################
#############################################################################################################################################################
###############generate_XML and generate_XML makes the same but the first one is for http petition and the second one for client petitions###################
#############################################################################################################################################################
#############################################################################################################################################################


def generate_XML (request):
	response = None	
	params = {}
	xmlPath = None
	channel = None
	category = None
	format = getResponseFormat (request) 
	#we get from the petition the parameters
	if request.method == "POST":
		if "fromx" in request.POST:
			params['fromx'] = request.POST['fromx']
	if request.method == "POST":		
		if "toy" in request.POST:
			params['toy'] = request.POST['toy']
	if request.method == "POST":
		if "channel" in request.POST:
			channel = request.POST['channel']		
			if "category" in request.POST:
				category = request.POST['category']
				channel = channel.lower()
				for field2 in request.POST:
						params[field2] = request.POST[field2]
				#after getting the parameters we generate the xml file from the external resource	
				xmlPath ,response, params2 = api_agregator.generate_XML(channel, category, params)
				#we manage the errors that we get
				if (xmlPath == None):
					list = response.split(" ")
					response =  error(format,"Wrong Post Parameters, " + list[1] + " missing")
			else:
				response =  error(format,"Wrong Post Parameters, category missing")
		else:
			response =  error(format,"Wrong Post Parameters, channel missing")														
	return response, xmlPath, channel, category

def generate_XML2 (parameters):
	response = None	
	params = {}
	xmlPath = None
	channel = None
	category = None
	#we get from the petition the parameters
	if "fromx" in parameters:
		params['fromx'] = parameters['fromx']			
	if "toy" in parameters:
		params['toy'] = parameters['toy']
	if "channel" in parameters:
		channel = parameters['channel']		
		if "category" in parameters:
			category = parameters['category']
			channel = channel.lower()
			for field2 in parameters:
					params[field2] = parameters[field2]
			#after getting the parameters we generate the xml file from the external resource	
			xmlPath ,response, params2 = api_agregator.generate_XML(channel, category, params)
			#we manage the errors that we get
			if (xmlPath == None):
				list = response.split(" ")
				response = "\"%s\" parameter is mandatory " % (list[1])
		else:
			response = "\category\ parameter is mandatory "
	else:
		response = "\category\ parameter is mandatory "													
	return response, xmlPath, channel, category

def error (format, message, code='500'):
	data = {'code'         : code,
		    'description'  : message,}
	return generateResponse(format, data, "error")

#############################################################################################################################################################
#############################################################################################################################################################
########layer_request and layer_node_request makes the same but the first one is for http petition and the second one for client petitions###################
#############################################################################################################################################################
#############################################################################################################################################################

def layer_request(request):
	format = getResponseFormat (request)
#	if not request.user.is_authenticated():
#		return error(format, "The user is not authenticated")

    #we generate the xml file
	response , xmlPath, channel, category = generate_XML(request)
	if (xmlPath == None):
		return response
	category = category.lower()
	#we get the layer using factory pattern
	layer = copy.deepcopy(CgeoNodeGenFactory.create_generator(category))
	#we generate the array and transform it into a HttpResponse
	finalResponse = layer.generate_all(channel, category,request)
	return finalResponse

def layer_node_request(parameters):
	#we generate the xml file
	response , xmlPath, channel, category = generate_XML2(parameters)
	if (xmlPath == None):
		return False, response
	category = category.lower()
	#we get the layer using factory pattern
	layer = copy.deepcopy(CgeoNodeGenFactory.create_generator(category))
	#we generate the array
	arrayList = layer.generate_Array(channel, category)
	return True, arrayList	
	